package com.jerry.android.Home.adapter;

import android.app.Activity;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.jerry.android.Common.MyApplication;
import com.jerry.android.Home.activity.MainActivity;
import com.jerry.android.R;
import com.jerry.android.Home.model.MainLevel0Item;
import com.jerry.android.Home.model.MainLevel1Item;
import com.jerry.android.Utils.ActivityUtils;

import java.util.List;

public class MainAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {

    public static final int TYPE_LEVEL_0 = 0;
    public static final int TYPE_LEVEL_1 = 1;

    private MainActivity mActivity;

    public MainAdapter(List<MultiItemEntity> data, Activity activity) {
        super(data);
        mActivity = (MainActivity) activity;
        addItemType(TYPE_LEVEL_0, R.layout.item_home_level0);
        addItemType(TYPE_LEVEL_1, R.layout.item_home_level1);
    }

    @Override
    protected void convert(final BaseViewHolder holder, final MultiItemEntity item) {
        switch (holder.getItemViewType()) {
            case TYPE_LEVEL_0:
                final MainLevel0Item lv0 = (MainLevel0Item)item;
                holder.setText(R.id.title, lv0.title)
                        .setImageResource(R.id.arrow, lv0.isExpanded() ? R.drawable.icon_arrows_down : R.drawable.icon_arrows_right);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getAdapterPosition();
                        if (lv0.isExpanded()) {
                            collapse(pos);
                        } else {
                            expand(pos);
                        }
                    }
                });
                break;
            case TYPE_LEVEL_1:
                final MainLevel1Item lv1 = (MainLevel1Item)item;
                holder.setText(R.id.title, lv1.title)
                        .setText(R.id.sub_title, lv1.subTitle);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ActivityUtils.getInstance().startActivity(mActivity, lv1.jumpClass, null);
                    }
                });
                break;
        }
    }
}
